package org.vijay.mvnbuilddownload;

import java.io.File;
import java.net.MalformedURLException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
/**
 * 
 *
 */
public class BuildDownloadTask extends Task
{
    private String sourceURL;
    private File toDir;
    private String toFile;
    private String downloadVersion;
    private String productName;
    private String extension;
    private boolean noversion;

    public void setSource(String source) {
        this.sourceURL=source;
    }

    public void setToFile(String toFile) {
        this.toFile=toFile;
    }

    public void setToDir(File toDir) {
        this.toDir=toDir;
    }

    public void setExtension(String extension) {
        this.extension = extension;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public void setDownloadVersion(String downloadVersion) {
        this.downloadVersion=downloadVersion;
    }

    public void setNoversion(boolean noversion) {
        this.noversion = noversion;
    }

    public void execute() throws BuildException {
        String name = getProject().getProperty("ant.project.name");

        log(name+" called.......");

        if (sourceURL == null) {
                throw new BuildException("No Source URL set.");
        }

        if (toDir == null) {
                throw new BuildException("No Destination Dir set.");
        }

        if (downloadVersion == null || downloadVersion.trim().length() <= 0) {
                throw new BuildException("Version not set.");
        }

        if (productName == null || productName.trim().length() <= 0) {
                throw new BuildException("Product Name not set.");
        }

        if (extension == null || extension.trim().length() <= 0) {
                throw new BuildException("Extension not set.");
        }

        //System.out.println("Source "+sourceURL);
        //System.out.println("productName "+productName);
        //System.out.println("downlaod "+downloadVersion);
        //System.out.println("Extension "+extension);
        FileLocator locator=new FileLocator(sourceURL, productName, downloadVersion, extension, noversion);
        //System.out.println(locator.locate());

        BinaryDownload download= new BinaryDownload(locator.locate());
        try {
            download.downloadFile(new File(toDir,((toFile==null) ? locator.getFileName() : toFile)));
        } catch (MalformedURLException ex) {
            System.out.println("Exception: "+ex.getMessage());
        }
        log("Done.......");
    }

    public static void main( String[] args )
    {
        BuildDownloadTask build=new BuildDownloadTask();
        /*build.setDownloadVersion("11.0.1-SNAPSHOT");
        build.setProductName("livexp-bas-api");
        build.setExtension("jar");
        build.setToDir(new File("c:\\"));
        build.setSource("http://repository.dnbgp.dnb.com/nexus/content/repositories/inhouse.snapshots/livexp/platform/livexp-bas-api/");
        build.execute();*/

        build.setDownloadVersion("10.0.1");
        build.setProductName("livexp-bas-api");
        build.setExtension("jar");
        build.setToDir(new File("c:\\"));
        build.setSource("http://repository.dnbgp.dnb.com/nexus/content/repositories/inhouse/livexp/platform/livexp-bas-api/");
        build.execute();
        //System.out.println( "Hello World!" );
    }
}
